package com.example.rentingapp.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.fragment.MineFragment;
import com.example.rentingapp.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;

//上传房源界面
public class MyHousePublishActivity extends BaseActivity implements View.OnClickListener {
    private Button btnPublish, btnBack;
    private EditText etLocation, etAddress, etHomeType, etTimeType, etHouseRent, etRenovation, etOther, etArea;
    private String houseUserId, location, address, homeType, timeType, houseRent, renovation, other, area;
    private ImageView imgHouse;

    @Override
    protected int initLayout() {
        return R.layout.activity_my_house_publish;
    }

    @Override
    protected void initView() {
        btnPublish = findViewById(R.id.btn_publish);
        btnBack = findViewById(R.id.btn_back);
        etLocation = findViewById(R.id.editText_location);
        etAddress = findViewById(R.id.editText_address);
        etHomeType = findViewById(R.id.editText_home_type);
        etTimeType = findViewById(R.id.editText_time_type);
        etHouseRent = findViewById(R.id.editText_house_rent);
        etRenovation = findViewById(R.id.editText_renovation);
        etOther = findViewById(R.id.editText_other);
        etArea = findViewById(R.id.editText_area);
        imgHouse = findViewById(R.id.img_house);
    }

    @Override
    protected void initData() {
        btnPublish.setOnClickListener(this);
        btnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_publish:
                publishHouse();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }

    private void publishHouse() {
        houseUserId = findByKey("user_id");
        location = etLocation.getText().toString().trim();
        address = etAddress.getText().toString().trim();
        homeType = etHomeType.getText().toString().trim();
        timeType = etTimeType.getText().toString().trim();
        houseRent = etHouseRent.getText().toString().trim();
        renovation = etRenovation.getText().toString().trim();
        other = etOther.getText().toString().trim();
        area = etArea.getText().toString().trim();

        if (StringUtils.isEmpty(location)) {
            showToast("请输入地区");
            return;
        }
        if (StringUtils.isEmpty(address)) {
            showToast("请输入详细地址");
            return;
        }
        if (StringUtils.isEmpty(houseRent)) {
            showToast("请输入租金");
            return;
        }

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("house_user_id", houseUserId);
        params.put("location", location);
        params.put("address", address);
        params.put("home_type", homeType);
        params.put("time_type", timeType);
        params.put("house_rent", houseRent);
        params.put("renovation", renovation);
        params.put("other", other);
        params.put("area", area);
        params.put("house_images", null);
        Api.config(ApiConfig.PUBLISH_HOUSE, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                showToastSync(res);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}